package com.shirley.dbExample.controller;

import com.shirley.dbExample.entity.Salary;
import com.shirley.dbExample.service.SalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/api/v1/salary")
public class SalaryController {

    @Autowired
    private SalaryService salaryService;

    @GetMapping
    @ResponseBody
    public ResponseEntity<List<Salary>> findPage(
            @RequestParam("page") Integer page,
            @RequestParam("size") Integer size) {
        return ResponseEntity.ok(salaryService.findPage(page, size).stream().collect(Collectors.toList()));
    }

    @PostMapping
    @ResponseBody
    public ResponseEntity<List<Salary>> saveBatch(@RequestBody List<Salary> salaries) {
        return ResponseEntity.ok(salaryService.saveAll(salaries));
    }

    @GetMapping("/{salaryId}")
    @ResponseBody
    public ResponseEntity<Salary> findOne(@PathVariable("salaryId") Long salaryId) {
        return ResponseEntity.ok(salaryService.find(salaryId));
    }
    @GetMapping("/employee/{employeeNumber}")
    @ResponseBody
    public ResponseEntity<List<Salary>> findByEmployeeNumber(@PathVariable("employeeNumber") String employeeNumber) {
        return ResponseEntity.ok(salaryService.findByEmployeeNum(employeeNumber));
    }
}
